package com.example.lee1.config;

import org.springframework.boot.autoconfigure.cache.CacheProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
@EnableCaching
@EnableConfigurationProperties(CacheProperties.class)
public class MyCacheConfig {

  //    @Autowired
  //    CacheProperties cacheProperties;

  @Bean
  public RedisCacheConfiguration redisCacheConfiguration(CacheProperties cacheProperties) {
    RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();
    config =
        config.serializeKeysWith(
            RedisSerializationContext.SerializationPair.fromSerializer(
                new StringRedisSerializer()));
    config =
        config.serializeValuesWith(
            RedisSerializationContext.SerializationPair.fromSerializer(
                new GenericJackson2JsonRedisSerializer()));

    CacheProperties.Redis redis = cacheProperties.getRedis();

    if (redis.getTimeToLive() != null) {
      config = config.entryTtl(redis.getTimeToLive());
    }
    if (redis.getKeyPrefix() != null) {
      config = config.prefixKeysWith(redis.getKeyPrefix());
    }
    if (!redis.isCacheNullValues()) {
      config = config.disableCachingNullValues();
    }
    if (!redis.isUseKeyPrefix()) {
      config = config.disableKeyPrefix();
    }
    return config;
  }

  //  @Bean
  //  public CacheManager cacheManager(RedisConnectionFactory connectionFactory) {
  //
  //    // 初始化json的序列化方式
  //    Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer =
  //        new Jackson2JsonRedisSerializer<>(Object.class);
  //    RedisSerializationContext.SerializationPair<Object> pair =
  //        RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer);
  //    // 设置 value 的序列化方式为 jackson2JsonRedisSerializer
  //    RedisCacheConfiguration defaultCacheConfig =
  //        RedisCacheConfiguration.defaultCacheConfig().serializeValuesWith(pair);
  //
  //    // 设置默认超过期时间是100秒
  //    defaultCacheConfig = defaultCacheConfig.entryTtl(Duration.ofSeconds(100));
  //
  //    // 初始化RedisCacheWriter
  //    RedisCacheWriter redisCacheWriter =
  //        RedisCacheWriter.nonLockingRedisCacheWriter(connectionFactory);
  //    // 初始化RedisCacheManager
  //    RedisCacheManager cacheManager = new RedisCacheManager(redisCacheWriter,
  // defaultCacheConfig);
  //
  //    // 设置白名单---非常重要********
  //    /*
  //    使用fastjson的时候：序列化时将class信息写入，反解析的时候，
  //    fastjson默认情况下会开启autoType的检查，相当于一个白名单检查，
  //    如果序列化信息中的类路径不在autoType中，
  //    反解析就会报com.alibaba.fastjson.JSONException: autoType is not support的异常
  //    可参考 https://blog.csdn.net/u012240455/article/details/80538540
  //     */
  //    // 解决查询缓存转换异常的问题
  //    ObjectMapper om = new ObjectMapper();
  //    om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
  //    om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
  //    jackson2JsonRedisSerializer.setObjectMapper(om);
  //
  //    return cacheManager;
  //  }
}
